package com.xing.dao;

import com.xing.core.BaseDao;
import com.xing.dto.ChatHistoryDTO;
import com.xing.dto.ChatUserDTO;
import com.xing.entity.ChatHistoryEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @version 1.0
 * @Author Xing
 * @Date 2024/4/23 14:46
 */
@Mapper
public interface ChatHistoryDao extends BaseDao<ChatHistoryEntity> {
    @Select("SELECT * FROM (SELECT *\n" +
            "FROM sys_chat_history\n" +
            "WHERE (received_id = #{receivedId}\n" +
            "    AND sender_id = #{senderId})\n" +
            "   or (sender_id = #{receivedId}\n" +
            "    AND received_id = #{senderId})\n" +
            "order by send_date DESC\n" +
            "LIMIT 10) as aa order by send_date")
    public List<ChatHistoryDTO>  getChatHistory(@Param("receivedId") Long receivedId,
                                                @Param("senderId") Long senderId,
                                                @Param("limit") Integer limit);

    @Select("SELECT u.id, u.nickname, s.url, COUNT(sender_id) number\n" +
            "FROM sys_chat_history h\n" +
            "         RIGHT JOIN sys_user u ON u.id = sender_id\n" +
            "         LEFT JOIN sys_oss s ON s.id = head_url\n" +
            "where received_id = #{receivedId} AND h.status = 1\n" +
            "group by sender_id;")
    public List<ChatUserDTO> getUsers(@Param("receivedId") Long receivedId);
}
